var layer;
var element;
var form;
var laydate;
var table;
var upload;
layui.use(['element','layer','laydate','form','table'], function(){
    layer = layui.layer;
    element = layui.element;
    form = layui.form;
    laydate = layui.laydate;
    table = layui.table;
    upload = layui.upload;
    load();
    selAuditList();
    //但是，如果你的HTML是动态生成的，自动渲染就会失效
    //因此你需要在相应的地方，执行下述方法来进行渲染
    form.render();

    //修改商品状态
    form.on('switch', function(data){
        var flag=data.elem.checked;
        console.log(flag);
        //获取商品状态
        var state=null;
        if(flag){
            state=1;
        }else{
            state=2;
        }
        //商品id
        var productId=data.elem.value;
        console.log(productId);
        $.ajax({
            url: "/SyOperationController/updProductSate",
            dataType: "json",
            type:"post",
            data:{"state":state,"productId":productId},
            success: function (data) {
                if(data.code==0){
                    layer.msg('修改成功',{icon: 1});
                }else{
                    layer.msg('修改失败',{icon: 2});
                }
            }
        });
    });

})
/**
 *  按钮  点击事件 刷新页面  返回  检索
 * */
function load() {
    //搜索按钮
    $("#indexBtn").bind('click', function () {
        //商品名称
        var product_name = $("input[name=product_name]").val();
        var search_keywords = $("input[name=search_keywords]").val();
        var currentprice_product = $("input[name=currentprice_product]").val();


        selAuditList(product_name,search_keywords,currentprice_product);
    })

    //重置
    $('#resetBtn').on('click',function(){
        selAuditList();
    })

    //刷新页面按钮
    $(".tit_btn").bind('click', function () {
        location.reload();
    })
}

//删除订单
function delAuditUser(productId){
    /*$.post("/SyOperationController/delPersonalProductList", {productId:productId}, function (res) {
        if(res.code==0){
            layer.msg("删除成功",{icon: 1});
            selAuditList();
        }else{
            layer.msg("删除失败",{icon: 2});
        }
    });*/
    $.post("/SyOperationController/upPersonalProductList", {productId:productId}, function (res) {
        if(res.code==0){
            layer.msg("删除成功",{icon: 1});
            selAuditList();
        }else{
            layer.msg("删除失败",{icon: 2});
        }
    });
}
//查看详情
function selAuditUser(productId) {
    var index = layer.open({
        type:2,
        title:"商品详情",
        area:['800px','600px'],
        shade:0.4,
        offset:'auto',
        maxmin: true,
        content:"/operation/comment/TeaCommodityView.html",
        success:function(layero,index) {
            //查找当前引入的iframe内容
            var obj = layero.find('iframe').contents();
            //根据选择器选择input,并设置值
            obj.find('#courseId').val(productId);

        }
    });
    layer.full(index);
}

//高级检索
function sousuo(){
    var productName1=$("input[name='productName1']").val();
    var searchKeywords1=$("input[name='searchKeywords1']").val();
    var productSate1=$("input[name='productSate1']").val();
    var state1=$("input[name='state1']").val();
    var currentpriceProduct1=$("input[name='currentpriceProduct1']").val();
    var user = parse("user");
    var uId = user.user_id;

    $.post("/SyOperationController/selPersonalProductList",
        {"uid":uId,"product_name":productName1,"search_keywords":searchKeywords1,"currentprice_product":currentpriceProduct1,"product_sate":productSate1,"state":state1},
        function (data) {
            userTab(data.data);
        });
}

//检索弹出层
function retrieval() {
    layer.open({
        type: 2,//弹出层的类型
        title: "高级检索",
        //area: ['715px', '610px'],//弹出层大小
        area: ['685px', '520px'],//弹出层大小
        shade: 0.4,//设置遮罩
        offset: 'auto',//设置遮罩的位置
        maxmin: true,
        //弹出层内容
        content: "PerBackstageUser_layer_index.html",
    })
    window.layer_1=layer;
}

//查询
function selAuditList(product_name,search_keywords,currentprice_product ){
    var user = parse("user");
    var uId = user.user_id;
    $.post("/SyOperationController/selPersonalProductList", {uid:uId,product_name:product_name,search_keywords:search_keywords,currentprice_product:currentprice_product}, function (res) {
        userTab(res.data);
    });
}

//展示列表
function userTab(item){
    table.render({
        elem:"#userListTab",//容器
        title:"个人商品列表",
        data:item,
        limit:5,//分页显示条数
        toolbar:"#toolbar_id",
        page:{
            layout:['prev','page','next','limit','skip','count'],
            limits:[5,10,15,20],
            first:"首页",
            last:"尾页",
        },
        cols:[[//定义列
            {type:'checkbox',align:'center',width:50},//复选框靠左
            {title:'序号',align:'center',width:60,templet:function(data){
                    return data.LAY_INDEX;
                }},
            {field:"main_image",title:"商品图片",align:'center',width:130,templet:function(data){
                    return "<img src='" +data.main_image+"'title='"+data.product_name+"' height='100px'>"
                }},
            {field:"product_name",title:"商品名称",align:'center',width:160},
            {field:"search_keywords",title:"关键字",align:'center'},
            {field:"currentprice_product",title:"现价",align:'center',templet:function(data){
                    return "￥"+data.currentprice_product;
                }},
            {field:"product_sate",title:"审核状态",align:'center',templet:function(data){
                    if(data.product_sate==2){//2审核中,3审核通过，4审核未通过
                        return "待审核"
                    }else if(data.product_sate==3){//2审核中,3审核通过，4审核未通过
                        return "审核通过"
                    }else if(data.product_sate==4){//2审核中,3审核通过，4审核未通过
                        return "审核未通过"
                    }else{//其他
                        return "其他"
                    }
                }},
            /*{field:"hits",title:"点击量",align:'center',templet:function(data){
                    if(data.product_sate==3){//2审核中,3审核通过，4审核未通过
                        if(data.hits==undefined){
                            return 0;
                        }else{
                            return data.hits;
                        }
                    }else{
                        return 0;
                    }
                }},
            {field:"collection_volume",title:"收藏量",align:'center',templet:function(data){
                    if(data.product_sate==3){//2审核中,3审核通过，4审核未通过
                        if(data.collection_volume==undefined){
                            return 0;
                        }else{
                            return data.collection_volume;
                        }
                    }else{
                        return 0;
                    }
                }},*/
            {field:"state",title:"上架状态",width:110,align:'center',templet:function(data){
                    if(data.product_sate==3){//2审核中,3审核通过，4审核未通过
                        if(data.state==1){
                            return  "<input type='checkbox' checked='' name='open' lay-skin='switch' lay-filter='switchTest' lay-text='上架|下架' value=\"" + data.product_id + "\">";
                        }else if(data.state==2){
                            return  "<input type='checkbox' name='close' lay-skin='switch' lay-text='上架|下架' value=\"" + data.product_id + "\">";
                        }else{
                            return  "<input type='checkbox' name='close' lay-skin='switch' lay-text='上架|下架' value=\"" + data.product_id + "\">";
                        }
                    }else{
                        return "<input type='checkbox' name='close' lay-skin='switch' lay-text='上架|下架' value=\"" + data.product_id + "\" disabled>";

                    }
                }},
            {field:"product_sales",title:"销量",align:'center',templet:function(data){
                    if(data.product_sate==3){//2审核中,3审核通过，4审核未通过
                        if(data.product_sales==undefined){
                            return 0;
                        }else{
                            return data.product_sales;
                        }
                    }else{
                        return 0;
                    }
                }},
            {field:"product_sate",title:"审核状态",align:'center', templet: function (d) {
                    if(d.product_sate==2){
                        return "<p style='margin-top:0px;'>待审核</p>" +
                            "<p><a onclick='javascript:ProductExamin(\""+d.product_id+"\");' style='color:#1ABC9C;cursor:pointer;text-decoration: none;'>审核详情</a></p>"
                    }else if(d.product_sate==3){
                        return "<p style='margin-top:0px;'>审核通过</p>" +
                            "<p><a onclick='javascript:ProductExamin(\""+d.product_id+"\");' style='color:#1ABC9C;cursor:pointer;text-decoration: none;'>审核详情</a></p>"
                    }else if(d.product_sate==4){
                        return "<p style='margin-top:0px;'>审核未通过</p>" +
                            "<p><a onclick='javascript:ProductExamin(\""+d.product_id+"\");' style='color:#1ABC9C;cursor:pointer;text-decoration: none;'>审核详情</a></p>"
                    }
                }},
            {field:"",title:"操作",align:'center',width:180,templet:function(data){
                    return "<p><a style='color:#009688;cursor:pointer;' onclick='selAuditUser(\""+data.product_id+"\")'>查看</a> "+
                        " <a style='color:#009688;cursor:pointer; margin-left: 15px;' onclick='ProductUpdate(\""+data.product_id+"\")'>编辑</a></p> "+
                        "<p><a style='color:#009688;cursor:pointer;' onclick='putProduct(\""+data.product_id+"\",\""+data.product_sate+"\")'>提交</a> "+
                        " <a style='color:#009688;cursor:pointer; margin-left: 15px;' onclick='delAuditUser(\""+data.product_id+"\")'>删除</a></p> ";
                }}
        ]]
    });
}

//点击商品审核
function ProductExamin(productId){
    localStorage.setItem("productId",productId); //商品ID
    auditLogging();
}
//审核详情
function auditLogging() {
    //$("#product_id").val(id);
    layer.open({
        type : 2,
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>审核详情</div>",
        area : [ '810px', '450px' ],
        maxmin: true,
        shade : 0.4,
        offset: 'auto',
        content :"/operation/ProductExamin.html",
        success:function(){
        }
    });
    //window.layer_1=layer;
}

//点击编辑
function ProductUpdate(productId){
    localStorage.setItem("productId",productId); //商品ID
    ProductUpdateLayer();
}
//弹出修改页面
function  ProductUpdateLayer() {
    layer.open({
        type : 2,//弹出层的类型
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>修改商品</div>",
        area : [ '1200px', '560px' ],
        maxmin: true,
        shade : 0.4,
        offset: 'auto',
        content :"PersonalProductList_update.html",
        success:function(layero,index){}
    });
    window.layer_1=layer;
}

//提交审核商品
function putProduct(productId,sate){
    if(sate==2){
        layer.msg("无需提交",{icon:6});
    }else if(sate==3){
        layer.msg("无需提交",{icon:6});
    }else{
        putProducts(productId,sate);
    }
}
//提交
function  putProducts(productId,sate){
    var productSate;
    if(sate==4){
        productSate=2;
    }
    $.post("/SyOperationController/upProductSate",
        {"productSate":productSate,"productId":productId},
        function (res) {
            layer.msg("提交成功",{icon:1});
            selAuditList();
        }
    );
}



